#include "include/Canvas.h"

/******************************
 *      Public
 ******************************/

    /******************************
     *      Constructors
     ******************************/

        Canvas::Canvas(string filePath, QWidget * parent) :   QWidget(){
            LoadImage(filePath);
            SetToolBox();
        }

        Canvas::Canvas(int width, int height, QWidget * parent )
            : QWidget(parent), m_Width(width), m_Height(height)
        {

            // Permet de déterminer si l'évenement de type mouseMoveEvent doit s'éxécuter même lorsque
            // qu'aucun bouton de la souris n'est enfoncé

            //setMouseTracking(true);

            // Je définis la valeur de zoom par défault à 1
            m_Zoom      =   1.0f;
            m_ZoomMax   =   100.0f;

            // J'initialise les valeurs permettant de définir le décalage entre le point 0,0
            // et le coin supérieur gauche de la fenêtre principale
            m_LeftOffset    =   0;
            m_TopOffset     =   0;

            // Creation de la vue chargée d'afficher les images chargées dans l'application
            m_Image         =   new QImage(width, height, QImage::Format_RGB32);
            m_LabelImage    =   new QLabel(this);
            m_LabelImage->setScaledContents(true);

            m_IsLastMoveEvent   =   false;
            SetToolBox();
            // Je charge l'image de fond du Canvas
            //m_Background    =   new AtlasImage(new QImage("../Ressources/bg"), "background");

        }

        void Canvas::Initialize(){

        }

    /******************************
     *      Methods
     ******************************/

        ToolBoxView & Canvas::GetToolBoxView(){
            return *m_ToolBox->m_View;
        }

        void Canvas::LoadImage(string filePath){

        }

        void Canvas::SaveImage(string filePath){

        }

        /******************************
         *      Accessors
         ******************************/

        void Canvas::Pixel(int x, int y, QColor &color){
            m_Image->setPixel(x,y,  color.rgba());
            m_LabelImage->setPixmap(QPixmap::fromImage(*m_Image));
        }

        /******************************
         *      Virtual
         ******************************/

            /******************************
             *      Events
             ******************************/

                void Canvas::resizeEvent(QResizeEvent *resize){
                    SetCanvas();
                }

                void Canvas::mouseReleaseEvent(QMouseEvent *mouseEvent){
                    if(mouseEvent->button() ==  Qt::LeftButton){
                    }
                    m_IsLastMoveEvent   =   false;
                }

                void Canvas::mouseMoveEvent(QMouseEvent * mouseEvent){
                    if(mouseEvent->buttons() ==  Qt::LeftButton){
                        if(m_IsLastMoveEvent){
                            m_LeftOffset    +=   mouseEvent->x() -m_LastX;
                            m_TopOffset     +=   mouseEvent->y() -m_LastY;

                            m_LastX         =   mouseEvent->x();
                            m_LastY         =   mouseEvent->y();

                           SetCanvas();

                        }else{
                            m_LastX =   mouseEvent->x();
                            m_LastY =   mouseEvent->y();

                            emit StatusChanged( QString::number(m_LastX));

                            printf("m_LastX %d \n", m_LastX);
                            m_IsLastMoveEvent   =   true;
                        }
                    }
                }

                void Canvas::wheelEvent(QWheelEvent * wheelEvent){
                    Zoom(wheelEvent->delta()*8.0f);
                }


/******************************
 *      Protected
 ******************************/

    /******************************
     *      Methods
     ******************************/

        /******************************
         *      Virtual
         ******************************/

            void Canvas::SetToolBox(){
                m_ToolBox   =   new ToolBox();
                m_ToolBox->m_Tools.push_back(new Tool("Tool1","Description1"));
                m_ToolBox->m_Tools.push_back(new Tool("Tool2","Description2"));
                m_ToolBox->m_Tools.push_back(new Tool("Tool3","Description3"));
                m_ToolBox->m_Tools.push_back(new BrushTool());
                m_ToolBox->CreateView();
            }

/******************************
 *      Private
 ******************************/

    /******************************
     *      Methods
     ******************************/

        void Canvas::SetCanvas(){
            int topLeftX    =   parentWidget()->width()/2 - (Width() *m_Zoom)/2.0  + m_LeftOffset;
            int topLeftY    =   parentWidget()->height()/2 - (Height()*m_Zoom)/2.0 + m_TopOffset;

            int width   =   m_Width     *   m_Zoom;
            int height  =   m_Height    *   m_Zoom;

            m_LabelImage->show();
            m_LabelImage->setPixmap(QPixmap::fromImage(*m_Image).scaled(width,height,Qt::IgnoreAspectRatio));

            m_LabelImage->setGeometry(topLeftX,topLeftY, width, height);

        }


/******************************
 *      Slots
 ******************************/

/***************************************
 *          Accessors
 ***************************************/

int Canvas::Width(){
    return m_Width;
}

int Canvas::Height(){
    return m_Height;
}

void Canvas::fromAtoB(int val, int & val2, int a, int b, int aa, int bb){

}

void Canvas::Zoom(float value){
    // On commence par appliquer un coefficient à la valeur passé en paramètre
    m_Zoom  +=  value * 0.0001;
    float zoomFactor    =   value * 0.0001;

    // Puis, on vérifie que la valeur de Zoom soit bien comprise entre  [zoomMin; ZoomMax]
    if(m_Zoom   > m_ZoomMax){
        m_Zoom  =   m_ZoomMax;


    }else if(m_Zoom <0.1){
        m_Zoom  =   0.1;
    }
    SetCanvas();
}
